package com.ww.core.widget;

import com.ww.core.R;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;

/**
 * 表格工具.
 * 如果�?��左右滑动，需加调用setOnTouchListener(final GestureDetector detector)
 * @version 2013-4-23
 * @author wwang
 */
public class DetailView extends LinearLayout {
	WebView w;
	Context context;

	public DetailView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		w = new WebView(context);
		w.setInitialScale(25);
		
		WebSettings webSetting = w.getSettings();
		//设置js可用  
		webSetting.setJavaScriptEnabled(true);
		webSetting.setBuiltInZoomControls(false);
		webSetting.setLightTouchEnabled(true);
		webSetting.setSupportZoom(false);
		setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
	}

	public DetailView(Context context) {
		super(context);
		w = new WebView(context);
		w.setInitialScale(25);
	}

	public void setOnTouchListener(final GestureDetector detector) {
		w.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				detector.onTouchEvent(event);
				return false;
			}
		});
	}

	/**
	 * 内容.
	 * @param html 富文本字符串
	 */
	public void setText(String html) {
		removeAllViews();
		StringBuffer s = new StringBuffer();
		s.append("<!DOCTYPE HTML ><html><head>");
		s.append("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>");
		s.append("<meta name='viewport' content='width=device-width,  initial-scale=1' />");
		s.append(style() + "</head> <body >");
		s.append(html);
		s.append("</body></html>");

	    w.getSettings().setDefaultTextEncodingName("utf-8") ;  
	    w.setBackgroundColor(2); // 设置背景色  
	    w.loadDataWithBaseURL(null, s.toString(), "text/html", "utf-8", null);
	    w.setVisibility(View.VISIBLE); // 加载完之后进行设置显示，以免加载时初始化效果不好看  
		this.addView(w);
	}

	private String style() {
		StringBuffer s = new StringBuffer();
		s.append("<style type='text/css'>");
		s.append("html,body {");
		s.append("margin: 0 auto;");
		s.append("padding: 5px;");
		s.append("}");
		s.append("</style>");
		return s.toString();
	}

}
